﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class Manage_SystemSetting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        CmsDataContext db = new CmsDataContext();
        var q = from aS in db.Setting
                orderby aS.ID
                select new { ID = aS.ID, Key = aS.SettingKey, Memo = aS.Description, Value = aS.Value };

        Settings.DataSource = q.ToList();
        Settings.DataBind();
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        List<string> values = new List<string>();
        List<string> keys = new List<string>();

        IEnumerator row = Settings.Controls.GetEnumerator();

        while (row.MoveNext())
        {
            RepeaterItem ri = (RepeaterItem)row.Current;

            IEnumerator col = ri.Controls.GetEnumerator();
            while (col.MoveNext())
            {
                object c = col.Current;
                if (c.GetType()== typeof(TextBox))
                {
                    values.Add(((TextBox)c).Text.Trim());
                }
                else if (c.GetType() == typeof(HiddenField))
                {
                    keys.Add(((HiddenField)c).Value.Trim());
                }
            }
        }

        //将键值对更新到数据库
        CmsDataContext db = new CmsDataContext();

        try
        {
            for (int i = 0; i < keys.Count; i++)
            {
                Setting setting = (from aS in db.Setting
                                   where aS.ID == keys[i]
                                   select aS).First();
                setting.Value = values[i];
            }

            db.SubmitChanges();

            MessageBox.Message = "参数设置保存成功。";
            MessageBox.Location = "./SystemSetting.aspx";
        }
        catch (Exception eee)
        {
            MessageBox.Message = "参数设置保存时发生错误：" + eee.Message;
        }
    }
}